<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Util\Tool;
use Illuminate\Support\Facades\DB;
class PlanTime extends Model
{
    protected $table = "c_plan_times";


    public function add($filter)
    {
        $plan_time = new PlanTime();
        $data = [];

        $times = Tool::handelString($filter['times']);
        if (empty($times)) {//全天不接单
            return true;
        }
        if (isset($filter['type']) && $filter['type'] == 3) {
            $plan_time->where([['work_id','=',$filter['work_id']],['dates','=',$filter['dates']],['type','=',1],['doctor_id','=',$filter['doctor_id']]])->delete();
            foreach ($times as $k => $v) {
                $dd = $plan_time->where([['times', '=', $v], ['work_id', '=', $filter['work_id']], ['dates', '=', $filter['dates']], ['type', '=', 1],['doctor_id','=',$filter['doctor_id']]])->first();
                if ($dd) {
                    continue;
                }
                $data[$k]['times'] = $v;
                $data[$k]['work_id'] = $filter['work_id'];
                $data[$k]['dates'] = $filter['dates'];
                $data[$k]['type'] = 1;
                $data[$k]['created_at'] = date("Y-m-d H:i:s", time());
                $data[$k]['updated_at'] = date("Y-m-d H:i:s", time());
            }
            DB::table('c_plan_times')->insert($data);
            $data = [];
            $plan_time->where([['work_id','=',$filter['work_id']],['dates','=',$filter['dates']],['type','=',2],['doctor_id','=',$filter['doctor_id']]])->delete();
            foreach ($times as $k => $v) {
                $dd = $plan_time->where([['times', '=', $v], ['work_id', '=', $filter['work_id']], ['dates', '=', $filter['dates']], ['type', '=', 2],['doctor_id','=',$filter['doctor_id']]])->first();
                if ($dd) {
                    continue;
                }
                $data[$k]['times'] = $v;
                $data[$k]['work_id'] = $filter['work_id'];
                $data[$k]['dates'] = $filter['dates'];
                $data[$k]['type'] = 2;
                $data[$k]['created_at'] = date("Y-m-d H:i:s", time());
                $data[$k]['updated_at'] = date("Y-m-d H:i:s", time());
            }
            $yes = DB::table('c_plan_times')->insert($data);
            if ($yes) {
                return true;
            }
        }
        $plan_time->where([['work_id','=',$filter['work_id']],['dates','=',$filter['dates']],['type','=',$filter['type']],['doctor_id','=',$filter['doctor_id']]])->delete();
        foreach ($times as $k => $v) {
            $dd = $plan_time->where([['times', '=', $v], ['work_id', '=', $filter['work_id']], ['dates', '=', $filter['dates']],['type','=',$filter['type']],['doctor_id','=',$filter['doctor_id']]])->first();
            if ($dd) {
                continue;
            }
            $data[$k]['times'] = $v;
            $data[$k]['work_id'] = $filter['work_id'];
            $data[$k]['dates'] = $filter['dates'];
            $data[$k]['type'] = $filter['type'];
            $data[$k]['created_at'] = date("Y-m-d H:i:s", time());
            $data[$k]['updated_at'] = date("Y-m-d H:i:s", time());
        }
        $yes = DB::table('c_plan_times')->insert($data);
        if ($yes) {
            return true;
        }
        return "添加失败";
    }

    /**
     * @param $filter
     * @return bool|string
     * 排单列表
     */
    public function getList($filter)
    {
        $plan_time = new PlanTime();
        $yes = $plan_time->
        where([['work_id', '=', $filter['work_id']], ['dates', '=', $filter['dates']], ['type', '=', $filter['type']],['doctor_id', '=', $filter['doctor_id']]])
            ->orderBy("times", "asc")->get();
        if (!empty($yes)) {
            $list = $yes->toArray();
            $ars = [];
            foreach ($list as $k => &$v) {
                if(strtotime($v['times']) > time()){
                    $v['times'] = substr($v['times'], 0, 5);
                    $ars[] = $v;
                }else{
                    unset($list[$k]);
                }
            }
            return $ars;
        }
        return [];
    }


}
